/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using System.IO;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Deletes project folder only if empty.
    /// </summary>
    public class DeleteEmptyProjectFolderAction : ActionBase
    {
        private string _folderPath = string.Empty;
        private EnvDTE.Project _project = null;

        #region Constructors
        public DeleteEmptyProjectFolderAction()
            : base() 
        { 
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        public string FolderPath
        {
            get
            {
                return this._folderPath;
            }
            set
            {
                this._folderPath = value;
            }
        }
        [Input(Required=true)]
        [CLSCompliant(false)]
        public EnvDTE.Project Project
        {
            get
            {
                return this._project;
            }
            set
            {
                this._project = value;
            }
        }        
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Get folder
            string pathToFolder = Path.Combine(DteHelper.BuildPath(this._project),
                (string)this.Evaluate(this._folderPath));
            ProjectItem folder = DteHelper.FindItemByPath(DTE.Solution, pathToFolder);
            if ((null == folder)
                || (folder.Kind != Constants.vsProjectItemKindPhysicalFolder))
                return;

            //Ensure not project items
            if (folder.ProjectItems.Count > 0)
                return;

            //Delete folder
            folder.Delete();
        }
        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._folderPath)
                throw new ArgumentNullException("FolderPath");
            if (0 == this._folderPath.Length)
                throw new ArgumentOutOfRangeException("FolderPath");
            if (null == this._project)
                throw new ArgumentNullException("Project");
        }
        #endregion
    }
}
